package nccloud.web.hr.login.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.sm.misc.BASE64Decoder;
import nc.bs.logging.Logger;
import nc.vo.pub.BusinessException;
import nc.vo.sm.UserRefreshTokenVO;
import nccloud.commons.lang.StringUtils;
import nccloud.framework.web.container.IHttpOperator;
import nccloud.utils.mobile_riart.MessageLogger;

/**
 * hr 移动端单点登录
 */
public class SSOLoginUtil {
	public  Object doLogin(IHttpOperator paramOperator, Map param) throws BusinessException {
		MessageLogger.info("第三方调用移动端审批通过【nccloud.web.hr.login.util.SSOLoginUtil】，开始调用NCC移动端单点登录...........");
		HRBindSSOLoginUtil util = new HRBindSSOLoginUtil();
		util.getUserInfoNcc(param);
		//add by chenjma HR移动端单点登录改造
		//UserRefreshTokenVO vo = util.getUserRefreshToken(param, false, true);
		UserRefreshTokenVO vo = util.getUserRefreshToken(param, true, true);
		if(vo==null){
			//查询结果为空，说明需要进行绑定
			MessageLogger.info("第三方调用移动端审批通过【nccloud.web.hr.login.util.SSOLoginUtil】，请先进行云账号绑定!.");
			throw new BusinessException("请先进行云账号绑定!");
		}
		Map<String, String> infoMap = new HashMap<>();

		String langCode = (String) param.get("langCode");
		if (StringUtils.isNotBlank(langCode)) {
			vo.setLangCode(langCode);// 取友空间的语种
		}
		infoMap.put("userCode", (String)param.get("user_code"));
		infoMap.put("NCCSN", (String) param.get("cuserid"));

		String encrypturl = (String) param.get("encrypturl");
		if (StringUtils.isNotBlank(encrypturl)) {
			try {
				infoMap.put("url", new String(new BASE64Decoder().decodeBuffer(encrypturl)));
			} catch (IOException e) {
				Logger.error("SSOLogin 加密url出错" + e.getMessage());
				MessageLogger.info("第三方调用移动端审批通过【nccloud.web.hr.login.util.SSOLoginUtil】，SSOLogin 加密url出错" + e.getMessage());
				throw new nc.bs.framework.json.core.exception.BusinessException("系统内部错误，请联系管理员");
			}
		}
		infoMap.put("busiCenterCode", (String) param.get("dataCenter"));
		return infoMap;
	}
}
